import type { IconFormatType, ThemeType } from '../../../types/interfaces'
import type { ICON_IDS } from './constants'

// ====================================================================================
//                                  图标配置
// ====================================================================================
export enum IconCategory {
  // 基础分类
  METADATA = 'metadata',
  NAVIGATION = 'navigation',
  ACTION = 'action',
  INTERFACE = 'interface',

  // 内容类型
  MEDIA = 'media',
  VIDEO = 'video',
  AUDIO = 'audio',
  IMAGE = 'image',
  FILE = 'file',
  DOCUMENT = 'document',
  LINK = 'link',

  // 平台类型
  PLATFORM = 'platform',
  SOCIAL = 'social',
  BLOG = 'blog',
  EDUCATION = 'education',
  ENTERTAINMENT = 'entertainment',
  KNOWLEDGE = 'knowledge',
  QNA = 'qna',
  WRITING = 'writing',

  // 技术相关
  DEVELOPMENT = 'development',
  PROGRAMMING = 'programming',
  OPEN_SOURCE = 'openSource',
  CLOUD = 'cloud',
  STORAGE = 'storage',
  DEVOPS = 'devops',
  TOOL = 'tool',
  AI = 'ai',
  AI_TOOL = 'aiTool',
  TECHNOLOGY = 'technology',

  // 用户相关
  ROLE = 'role',
  USER = 'user',
  AUTH = 'auth',
  COMMUNICATION = 'communication',

  // 系统状态
  SYSTEM = 'system',
  STATUS = 'status',
  ERROR = 'error',
  RATING = 'rating',
  BADGE = 'badge',

  // 商业相关
  COMMERCE = 'commerce',
  PRODUCTIVITY = 'productivity',

  // 网络相关
  NETWORK = 'network',
  BROWSER = 'browser',

  // 时间相关
  TIME = 'time',

  // 其他
  OTHER = 'other',
}

export enum IconTag {
  LINK = 'link',
  VIDEO_SOURCE = 'video-source',
  ARTICLE_LINK = 'article-link',
}

/**
 * 图标注册类型定义
 */
export type IconRegistry = Partial<{
  [category in IconCategory]: {
    [K in string]: K
  }
}>

/**
 * 递归提取图标 ID
 */
type ExtractIconIds<T> = T extends object
  ? { [K in keyof T]: ExtractIconIds<T[K]> }[keyof T]
  : T

export type IconId = ExtractIconIds<typeof ICON_IDS>

export type IconAttributes = Record<string, string>

export interface IconMetadata {
  viewBox?: string
  fill?: string
  attributes?: IconAttributes
  size?: number
  format?: IconFormatType
}

export interface IconMatchPattern {
  text?: MatchPattern
  link?: MatchPattern
  domain?: string[]
}

interface BaseIconConfig {
  id: IconId
  name: string
  category: IconCategory[]
  tag?: IconTag[]
  metadata?: IconMetadata
  match?: IconMatchPattern
  priority?: number
}

// 路径类型的图标配置
interface PathIconConfig extends BaseIconConfig {
  path: string | ((theme: ThemeType | undefined) => string)
  iconGenerator?: never // 确保不能同时存在
}

// 生成器类型的图标配置
interface GeneratorIconConfig<T extends any[] = any[]> extends BaseIconConfig {
  path?: never // 确保不能同时存在
  iconGenerator: (...args: T) => string
  generatorParams?: T // 可选：定义参数类型
}

// 联合类型 - 路径和生成器二选一
export type IconConfig = PathIconConfig | GeneratorIconConfig

export interface IconSvgOptions {
  theme?: ThemeType
  className?: string
  size?: number
  format?: IconFormatType
  generatorArgs?: any[]
}

// ====================================================================================
//                                  图标统计
// ====================================================================================
export interface IconValidationResult {
  isValid: boolean
  errors: string[]
  warnings: string[]
}

export interface IconStats {
  total: number
  byCategory: Record<string, number>
  categoryTree: Record<IconCategory, number>
  pathStats: {
    totalPaths: number
    pathLengths: {
      min: number
      max: number
      average: number
      distribution: Record<string, number> // 长度分布
    }
    complexIcons: number // 路径长度超过平均值的图标
    simpleIcons: number // 路径长度低于平均值的图标
  }
  viewBoxStats: {
    uniqueViewBoxes: string[]
    viewBoxDistribution: Record<string, number>
  }
  sizeStats: {
    defaultSizes: number[]
    sizeDistribution: Record<number, number>
    missingSizes: number // 没有设置size的图标数量
  }
  formatStats: {
    formatDistribution: Record<IconFormatType, number>
    cssUrlIcons: string[] // 使用CSS_URL格式的图标ID
    nonCssUrlIcons: string[] // 不使用CSS_URL格式的图标ID
  }
  duplicatePaths: Map<string, string[]> // 相同路径的图标ID映射
  colorVariants: Map<string, Array<{ id: string, fill: string }>> // 颜色变体映射
}

// ====================================================================================
//                                 脚本 VIP 用户图标
// ====================================================================================
export type VipIconThemeType = 'pink' | 'gold' | 'black' | 'purple' | 'blue' | 'red' | 'green' | 'orange' | 'rainbow' | 'aurora'
export interface VipIconThemeConfig {
  right: string
  left: string
  text: string
}

// ====================================================================================
//                                 平台 VIP 用户图标
// ====================================================================================
export type VipUserIconColorConfig = {
  [key in ThemeType]: {
    diamond: {
      base: string // 钻石底色
      highlight: string // 高光色
      shadow?: string // 阴影色（可选）
    }
    crown: {
      left: string // 皇冠左侧颜色
      right: string // 皇冠右侧颜色
      jewel: string // 皇冠宝石色
    }
    lightning: {
      base: string // 闪电主色
      top: string // 闪电顶部颜色
      bottom: string // 闪电底部颜色
    }
  };
}
